package com.neusoft.actor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.actor.dao.BedUsageDao;
import com.neusoft.actor.dto.BedUsageDetailDto;
import com.neusoft.actor.po.BedUsage;
import com.neusoft.actor.po.ResponseBean;
import com.neusoft.actor.service.BedUsageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class BedUsageServiceImpl implements BedUsageService {

    @Autowired
    private BedUsageDao bedUsageDao;

    @Override
    public ResponseBean<List<BedUsage>> getAllBedUsages() {
        List<BedUsage> bedUsages = bedUsageDao.selectList(null);
        return new ResponseBean<>(bedUsages);
    }

    @Override
    public ResponseBean<BedUsage> getBedUsageById(Integer usageId) {
        BedUsage bedUsage = bedUsageDao.selectById(usageId);
        if (bedUsage != null) {
            return new ResponseBean<>(bedUsage);
        } else {
            return new ResponseBean<>(404, "床位使用记录未找到");
        }
    }

    @Override
//    按客户id查询床位使用记录，正在使用
    public ResponseBean<List<BedUsage>> getBedUsageByClientId(Integer clientId) {
        List<BedUsage> bedUsages = bedUsageDao.selectList(
                new QueryWrapper<BedUsage>().eq("usage_id", clientId).eq("is_current", 1)
        );
        if (bedUsages != null) {
            return new ResponseBean<>(bedUsages);
        } else {
            return new ResponseBean<>(404, "床位使用记录未找到");
        }
    }

    @Override
//    多条件查询，连接床位表，客户表
    public ResponseBean<List<BedUsageDetailDto>> getBedUsageDetails(String clientName, String admissionDate, Boolean isCurrent) {
        List<BedUsageDetailDto> bedUsageDetailDtos = bedUsageDao.getBedUsageDetailsByConditions(clientName, admissionDate, isCurrent);
        if (bedUsageDetailDtos != null) {
            return new ResponseBean<>(bedUsageDetailDtos);
        } else {
            return new ResponseBean<>(404, "床位使用记录未找到");
        }
    }

    @Override
    public ResponseBean<List<BedUsage>> getBedUsageByCondition(Integer id, LocalDate startDate, boolean isCurrent) {
        QueryWrapper<BedUsage> queryWrapper = new QueryWrapper<>();
        if (id != null) {
            queryWrapper.eq("client_id", id);
        }
        if (startDate != null) {
            queryWrapper.eq("start_date", startDate);
        }
        if (startDate != null) {
            queryWrapper.eq("isCurrent", startDate);
        }
        List<BedUsage> bedUsages = bedUsageDao.selectList(queryWrapper);
        return new ResponseBean<>(bedUsages);
    }

    @Override
    public ResponseBean<Integer> addBedUsage(BedUsage bedUsage) {
        int result = bedUsageDao.insert(bedUsage);
        return new ResponseBean<>(result); // 返回插入成功的行数
    }

    @Override
    public ResponseBean<Integer> updateBedUsage(BedUsage bedUsage) {
        int result = bedUsageDao.updateById(bedUsage);
        return new ResponseBean<>(result); // 返回更新成功的行数
    }

    @Override
    public ResponseBean<Integer> deleteBedUsage(Integer usageId) {
        int result = bedUsageDao.deleteById(usageId);
        return new ResponseBean<>(result); // 返回删除成功的行数
    }
}